package com.devbobcorn.nekoration.blocks;

import java.util.Collections;
import java.util.List;

import javax.annotation.Nonnull;

import com.devbobcorn.nekoration.NekoColors;
import com.devbobcorn.nekoration.common.VanillaCompat;
import com.devbobcorn.nekoration.items.HalfTimberBlockItem;

import net.minecraft.util.math.BlockPos;
import net.minecraft.util.Hand;
import net.minecraft.util.ActionResult;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.Properties;
import net.minecraft.state.property.IntProperty;
import net.minecraft.loot.context.LootContext;
import net.minecraft.util.hit.BlockHitResult;

public class HalfTimberBlock extends Block {
    public static final IntProperty COLOR0 = Properties.LEVEL_15;
    public static final IntProperty COLOR1 = Properties.AGE_15;

    public HalfTimberBlock(Settings settings) {
        super(settings);
        this.setDefaultState(this.stateManager.getDefaultState().with(COLOR0, 2).with(COLOR1, 14));
    }

    protected void appendProperties(StateManager.Builder<Block, BlockState> s) {
        s.add(COLOR0, COLOR1);
    }

    public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
        ItemStack itemStack = player.getStackInHand(hand);

        if (world.isClient) {
            return (VanillaCompat.COLOR_ITEMS.containsKey(itemStack.getItem()) || VanillaCompat.RAW_COLOR_ITEMS.containsKey(itemStack.getItem())) ?
            ActionResult.SUCCESS : ActionResult.PASS;
        }

        if (VanillaCompat.RAW_COLOR_ITEMS.containsKey(itemStack.getItem())) {
            world.setBlockState(pos, state.with(COLOR1, VanillaCompat.RAW_COLOR_ITEMS.get(itemStack.getItem())), 3);
            return ActionResult.SUCCESS;
        }
        if (VanillaCompat.COLOR_ITEMS.containsKey(itemStack.getItem())) {
            world.setBlockState(pos, state.with(COLOR0, VanillaCompat.COLOR_ITEMS.get(itemStack.getItem())), 3);
            return ActionResult.SUCCESS;
        }
        return ActionResult.PASS;
    }

    @Override
    public BlockState getPlacementState(ItemPlacementContext ctx) {
        ItemStack stack = ctx.getStack();
        if (stack.getItem() instanceof HalfTimberBlockItem)
            return this.getDefaultState().with(COLOR0, HalfTimberBlockItem.getColor0(stack).getNBTId()).with(COLOR1, HalfTimberBlockItem.getColor1(stack).getNBTId());
        return this.getDefaultState();
    }

    @Nonnull
    @Override
    public ItemStack getPickStack(BlockView world, BlockPos pos, BlockState state) {
        ItemStack stack = new ItemStack(this.asItem());
        HalfTimberBlockItem.setColor0(stack, NekoColors.EnumWoodenColor.getColorEnumFromID(state.get(COLOR0).byteValue()));
        HalfTimberBlockItem.setColor1(stack, NekoColors.EnumNekoColor.getColorEnumFromID(state.get(COLOR1).byteValue()));
        return stack;
    }

    @Override
    public List<ItemStack> getDroppedStacks(BlockState state, LootContext.Builder builder) {
        ItemStack stack = new ItemStack(this.asItem());
        HalfTimberBlockItem.setColor0(stack, NekoColors.EnumWoodenColor.getColorEnumFromID(state.get(COLOR0).byteValue()));
        HalfTimberBlockItem.setColor1(stack, NekoColors.EnumNekoColor.getColorEnumFromID(state.get(COLOR1).byteValue()));
        return Collections.singletonList(stack);
    }
}